package com.liusiqi.framework.context.support;

import com.liusiqi.framework.beans.factory.support.BeanDefinitionReader;
import com.liusiqi.framework.beans.factory.support.BeanDefinitionRegistry;
import com.liusiqi.framework.context.ApplicationContext;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: sql
 * @date: 2024/2/22
 * @Description:创建容器对象，加载配置文件，初始化bean
 * @version: 1.0
 */
public abstract class AbstractApplicationContext implements ApplicationContext {

    protected BeanDefinitionReader beanDefinitionReader;

    protected Map<String, Object> singletonObjects = new HashMap<>();

    protected  String configLocation;

    @Override
    public void refresh() {
        //加载BeanDefinition对象
        try {
            beanDefinitionReader.loadBeanDefinitions(configLocation);
            //初始化bean对象
            finishInitialization();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void finishInitialization() throws Exception{
        //获取注册表对象
        BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry();
        String[] beanDefinitionNames = registry.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            getBean(beanDefinitionName);
        }
    }
}
